package cn.stylefeng.guns.config.web;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 监听所有db的过期事件__keyevent@*__:expired"
 *
 * @author lsm
 */
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {



    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * 针对redis数据失效事件，进行数据处理
     *
     * @param message
     * @param pattern
     */
    @Override
    @Transactional
    public void onMessage(Message message, byte[] pattern) {
        // 获取过期Key
        String expiredKey = message.toString();
        // 获取key前缀
        String keyPrefix = expiredKey.substring(0, expiredKey.indexOf(":"));
        // 截取出id
        String dataId = expiredKey.substring(expiredKey.indexOf(":") + 1);

    }
}
